/*
 * @lc app=leetcode.cn id=747 lang=javascript
 *
 * [747] 至少是其他数字两倍的最大数
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @return {number}
 */
var dominantIndex = function (nums) {
  if (nums.length === 0) return -1;
  if (nums.length === 1) return 0;
  let index = 0;
  let [max1] = nums;
  let max2 = -Number.MAX_SAFE_INTEGER;
  for (let i = 1; i < nums.length; i++) {
    if (nums[i] > max1) {
      index = i;
      max2 = max1;
      max1 = nums[i];
    } else if (nums[i] > max2) {
      max2 = nums[i]
    }
  }
  return max1 >= 2 * max2 ? index : -1;
};
// @lc code=end

